//
//  ViewController.m
//  DesignModel
//
//  Created by 赵明明 on 2022/1/5.
//

#import "ViewController.h"
#import "Singleton.h"
#import "Factory.h"
#import "prodbase.h"
#import "prod1.h"
#import "prod2.h"
#import "ProdIterator.h"
#import "Mediator.h"
#import "Person.h"
#import "TemplateBase.h"
#import "TemplateChild1.h"
#import "TemplateChild2.h"
#import "BaseWork.h"
#import "Visit1.h"
#import "Visit2.h"

@implementation ViewController
- (IBAction)getClone:(id)sender {
    // Do any additional setup after loading the view.
    Person * p1= [[Person alloc] initWithNameAddress:@"zhaoyi":@"beijing" ];
    Person * p2 = [[Person alloc] initWithNameAddress:@"zhaoer" :@"beijing" ];
    NSLog(@"p1.name= %@,p2.name=%@",p1.name,p2.name );
    
    Person * p3=[p1 copy];
    p3.name=@"zhaosan";
    NSLog(@"p1.name= %@,p3.name=%@",p1.name,p3.name );
    NSLog(@"p3 is a copy of p1, we init p3 with little time,less operation!!!");
}
- (IBAction)getMediator:(id)sender {
    
    // 中介者模式
    Mediator * me = [[Mediator alloc] myinit];
    prod1 * p1 = [[prod1 alloc] myinit:me];
    prod2 * p2 = [[prod2 alloc] myinit:me];
    [p1 sendMessage:@" good?"];
    [p2 sendMessage:@" I am not good!"];
}
- (IBAction)getIterator:(id)sender {
    // 迭代器模式
    NSMutableArray* ma=[[NSMutableArray alloc]init];
    [ma addObject:@"prod1"];
    [ma addObject:@"prod2"];
    [ma addObject:@"prod3"];
    [ma addObject:@"prod4"];
    
    ProdIterator* pi = [[ProdIterator alloc] myinit:ma];
    while(![pi isDone]){
        NSLog(@"%@",[pi Current]);
        [pi Next];
    }
}
- (IBAction)getFactory:(id)sender {
    // 工厂模式
    ProdBase * prod1 = [Factory create:@"prod1"];
    [prod1 showName];
    ProdBase * prod2 = [Factory create:@"prod2"];
    [prod2 showName];
   
}
- (IBAction)getSingleton:(id)sender {
    
    // 单例模式
    NSLog(@"https://blog.csdn.net/michaelkiven/article/details/50342469");
    Singleton * singleton = [Singleton getInstance];
    NSLog(@"getInstance: %d",singleton.mInt);
    Singleton * singleton1 = [[Singleton alloc ] init];
    NSLog(@"alloc init: %d",singleton1.mInt);
    
}
- (IBAction)getTemplate:(id)sender {
    // 第一种实现
    TemplateChild1 * c1 = [[TemplateChild1 alloc]init];
    [c1 IceElephent];
    
    // 第二种实现
    TemplateChild2 * c2 = [[TemplateChild2 alloc]init];
    [c2 IceElephent];
}
- (IBAction)getVisitor:(id)sender {
    // 基本工作
    BaseWork *bw = [[BaseWork alloc] init];
    
    // 访问者1 利用acceptVisitor函数把工作做了
    Visit1* v1=[[Visit1 alloc] init];
    [bw acceptVisitor:v1];
    
    // 访问者2 利用acceptVisitor函数把工作做了
    Visit2* v2=[[Visit2 alloc] init];
    [bw acceptVisitor:v2];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
}


- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}


@end
